import { bangdanStore, playerStore } from '../../../store/index'
import { getSongDetail } from '../../../service/api_music'

const bangdans = {
	'新歌榜': 'newBangdans',
	'热门榜': 'hotBangdans',
	'原创榜': 'originBangdans',
	'飙升榜': 'upBangdans'
}
Page({
	data: {
		state: '',
		songInfo: {},
		type: ''
	},
	onLoad(options) {
		const type = options.type
		this.setData({ type })
		if(type === 'bd'){
			const state = bangdans[options.bangdan]
			this.setData({ state })
			bangdanStore.onState(state, this.getBangdanDataHandler)
		}else if(type === 'id') {
			const id = options.id
			getSongDetail(id).then(res => {
				this.setData({songInfo: res.playlist})
			})
		}
	},
	onUnload() {
		if(!this.data.state) return
		bangdanStore.offState(this.data.state, this.getBangdanDataHandler)
	},
	getBangdanDataHandler(res) {
		this.setData({ songInfo: res })
	},
	// song-item-v1的点击
	handleSongItemClick(event) {
		const index = event.currentTarget.dataset.index
		playerStore.setState('playSongsList', this.data.songInfo.tracks)
		playerStore.setState('playSongIndex', index)
	}
})